

#include <cegfx/ceeffect.h>
#include <cegfx/cematerial.h>
#include <stdio.h>

ceMaterial::ceMaterial ()
	: effect(0)
	, diffuse (0)
	, specular (0)
	, bump (0)
{
	IQF_CONSTRUCT;

	ZeroMemory (&material, sizeof (material));
	material.Diffuse.r = material.Specular.r = 1.0f;
	material.Diffuse.g = material.Specular.g = 1.0f;
	material.Diffuse.b = material.Specular.b = 1.0f;
	material.Diffuse.a = material.Specular.a = 1.0f;
	material.Power = 64.0f;
}

ceMaterial::~ceMaterial ()
{
	SAVE_RELEASE (effect);
}

IQF_IMPLEMENTATION_BEGIN(ceMaterial);
IQF_IMPLEMENTATION_END();

void ceMaterial::SetAmbient (const D3DCOLORVALUE& color)
{
	material.Ambient = color;
}

void ceMaterial::SetDiffuse (const D3DCOLORVALUE& color)
{
	material.Diffuse = color;
}

void ceMaterial::SetSpecular(const D3DCOLORVALUE& color)
{
	material.Specular = color;
}

void ceMaterial::SetEmissive (const D3DCOLORVALUE& color)
{
	material.Emissive = color;
}

void ceMaterial::SetPower (float power)
{
	material.Power = power;
}

const D3DCOLORVALUE& ceMaterial::GetAmbient () const
{
	return material.Ambient;
}

const D3DCOLORVALUE& ceMaterial::GetDiffuse () const
{
	return material.Diffuse;
}

const D3DCOLORVALUE& ceMaterial::GetSpecular () const
{
	return material.Specular;
}

const D3DCOLORVALUE& ceMaterial::GetEmissive () const
{
	return material.Emissive;
}

float ceMaterial::GetPower () const
{
	return material.Power;
}

void ceMaterial::SetDiffuseTexture (LPDIRECT3DTEXTURE9 diffuse)
{
	SAVE_SET(this->diffuse, diffuse);
}

void ceMaterial::SetSpecularTexture (LPDIRECT3DTEXTURE9 specular)
{
	SAVE_SET(this->specular, specular);
}

void ceMaterial::SetBumpTexture (LPDIRECT3DTEXTURE9 bump)
{
	SAVE_SET(this->bump, bump);
}

LPDIRECT3DTEXTURE9 ceMaterial::GetDiffuseTexture ()
{
	return diffuse;
}

LPDIRECT3DTEXTURE9 ceMaterial::GetSpecularTexture ()
{
	return specular;
}

LPDIRECT3DTEXTURE9 ceMaterial::GetBumpTexture ()
{
	return bump;
}




void ceMaterial::SetEffect (ceEffect* effect)
{
	SAVE_SET(this->effect, effect);
}

ceEffect* ceMaterial::GetEffect ()
{
	return effect;
}

void ceMaterial::ApplyStates (LPDIRECT3DDEVICE9 device)
{
	if (effect)
	{
		effect->SetMaterial (material);
		if (diffuse)
		{
			effect->SetDiffuseTexture (diffuse);
		}
		if (specular)
		{
			effect->SetSpecularTexture (specular);
		}
		if (bump)
		{
			effect->SetBumpTexture (bump);
		}
	}
	else
	{
		device->SetMaterial (&material);
		if (diffuse)
		{
			device->SetTexture (0,  diffuse);
		}
		if (specular)
		{
			device->SetTexture (1, specular);
		}
	}
}


unsigned ceMaterial::Begin (LPDIRECT3DDEVICE9 device)
{
	unsigned passes = 1;
	device->SetMaterial (&material);
	if (effect)
	{
		passes = effect->Begin ();
	}
	return passes;
}


void ceMaterial::BeginPass (unsigned pass)
{
	if (effect)
	{
		effect->BeginPass (pass);
	}
}

void ceMaterial::EndPass ()
{
	if (effect)
	{
		effect->EndPass ();
	}
}


void ceMaterial::End ()
{
	if (effect)
	{
		effect->End ();
	}
}


